package com.wyt.controller.empcollor;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyt.dao.EmpAndDeptAndRolMapper;
import com.wyt.dao.EmployeeMapper;
import com.wyt.dao.LeftMapper;
import com.wyt.entity.*;
import com.wyt.until.CommonResult;
import com.wyt.until.TableLayui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @program: xiangmu01
 * @description: 员工表功能
 * @author: 王一同
 * @create: 2021-11-23 08:42
 **/

@RestController

public class ShowEmpController {
    @Autowired
    private EmpAndDeptAndRolMapper eadMapper;

    @Autowired
    private  EmployeeMapper empMapper;

    @Autowired
    private LeftMapper lempMapper;

    /**
    	* @Description:    部门，员工，职位三表联查
    	* @Author:         wyt
    	* @CreateDate:     2021/11/23 20:28
        * @UpdateRemark:   特殊说明
    	*/

    @RequestMapping("findAllEmp")
    public TableLayui findAllEmp(Integer limit,Integer page,String employeename,String depname,String pername){
        PageHelper.startPage(page,limit);
        List<EmpAndDeptAndRol> empAndDeptAndRols = eadMapper.selectEmpAndDeptAndRolAll(employeename,depname,pername);
        PageInfo<EmpAndDeptAndRol> pageInfo=new PageInfo<EmpAndDeptAndRol>(empAndDeptAndRols);
        TableLayui tableLayui = new TableLayui(0, "",  pageInfo.getTotal(), pageInfo.getList());
        return tableLayui;
    }

    /**
    	* @Description:    修改，新增员工表
    	* @Author:         wyt
    	* @CreateDate:     2021/11/24 15:23
        * @UpdateRemark:   用同一张表单，区分eName
    	*/

    @RequestMapping("updateemp")
    public CommonResult updateemp(IUemp emp){
        CommonResult commonResult = new CommonResult();
        if(emp.getEnumber()==null){
            int i = empMapper.insert(emp);
            if(i>=1){
                commonResult.setCode(200);
                commonResult.setMsg("操作成功");
                return commonResult;
            }else{
                commonResult.setCode(500);
                commonResult.setMsg("操作失败");
                return commonResult;
            }
        }else{
            int i = empMapper.updateByPrimaryKey(emp);
            if(i>=1){
                commonResult.setCode(200);
                commonResult.setMsg("操作成功");
                return commonResult;
            }else{
                commonResult.setCode(500);
                commonResult.setMsg("操作失败");
                return commonResult;
            }
        }

    }
    /**
    	* @Description:    离职提交
    	* @Author:         wyt
    	* @CreateDate:     2021/11/26 16:11
        * @UpdateRemark:   特殊说明
    	*/
    @RequestMapping("lizhiemp")
    public CommonResult lizhiemp(IUemp emp){
        System.out.println(emp);
        CommonResult commonResult = new CommonResult();
        Integer enumber = emp.getEnumber();
        int i = empMapper.deleteByPrimaryKey(enumber);
        String ename = emp.getEname();
        String esex = emp.getEsex();
        String etel = emp.getEtel();
        String erole = emp.getErole();
        String did = emp.getDid();
        Date eIndate = emp.getEIndate();
        int lzyg = lempMapper.lzyg(ename, esex, etel, erole, did,eIndate);
        if(i>=1&&lzyg>=1){
            commonResult.setCode(200);
            commonResult.setMsg("离职成功");
            return commonResult;
        }else{
            commonResult.setCode(500);
            commonResult.setMsg("离职失败");
            return commonResult;
        }


    }

    /**
    	* @Description:    根据eNumber删除
    	* @Author:         wyt
    	* @CreateDate:     2021/11/24 15:24
    	*/

    @RequestMapping("deleteemp")
    public CommonResult updateemp(Integer enumber ){
        int i = eadMapper.deleteByEnumber(enumber);
        CommonResult commonResult = new CommonResult();
        if(i>=1){
            commonResult.setCode(200);
            commonResult.setMsg("删除成功");
            return commonResult;
        }else{
            commonResult.setCode(500);
            commonResult.setMsg("删除失败");
            return commonResult;
        }
    }

    /**
    	* @Description:    用于切换员工在职状态
    	* @Author:         wyt
    	* @CreateDate:     2021/11/24 15:25
        * @UpdateRemark:   通过enumber切换eStatus
    	*/

    @RequestMapping("updateStatus")
    public CommonResult updateStatus(Integer eStatus,Integer eNumber){
        if(eStatus==1){
            eStatus=0;
        }else{
            eStatus=1;
        }
        int i = empMapper.updateStatus(eStatus, eNumber);
        CommonResult commonResult = new CommonResult();
        if(i>=1){
            commonResult.setCode(200);
            commonResult.setMsg("切换成功");
            return commonResult;
        }else{
            commonResult.setCode(500);
            commonResult.setMsg("切换失败");
            return commonResult;
        }
    }

    /**
    	* @Description:    获取全部离职员工
    	* @Author:         wyt
    	* @CreateDate:     2021/11/24 15:26
        * @UpdateRemark:   特殊说明
    	*/

    @RequestMapping("getAllLEmp")
    public TableLayui getAllLEmp(Integer page, Integer limit,String employeename,String depname,String pername){
        PageHelper.startPage(page,limit);
        List<Left> allLemp = lempMapper.findAllLemp(employeename,depname,pername);
        PageInfo<Left> pageInfo=new PageInfo<Left>(allLemp);
        TableLayui tableLayui = new TableLayui(0, "",  pageInfo.getTotal(), pageInfo.getList());
        return tableLayui;

    }

}
